package work.comeback.omo.util.common;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;

public class CookieUtils {

    /**
     * 获取指定名字的cookie
     *
     * @param request HttpServletRequest 对象
     * @param name    cookie 名字
     * @return 返回cookie对象
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }
        return null;
    }

    /**
     * 根据名字和值创建一个新的cookie
     *
     * @param name  cookie 名字
     * @param value cookie 值
     * @return 新生成的cookie对象
     */
    public static Cookie createCookie(String name, String value) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        return cookie;
    }
}
